# 协程：本质就是线程
# 协程：用户态的(可控的)
# 进程、线程：由操作系统调度的(不可控的)

# 进程>线程>协程
# 协程如何实现并发？


# 中断
# def funa():
#     print(123)
#     print(456)
#     print(789)
#
# def funb():
#     print("aaa")
#     print("bbb")
#     print("ccc")
# 123 aaa 456 bbb 789 ccc
# aaa bbb 123 456 ccc 789

# 协程：开销会比进程甚至比线程更小
import time
def funa():
    num = 0
    while True:
        num+=1
        yield num
        time.sleep(1)
def funb():
    num = 0
    while True:
        num-=1
        yield num
        time.sleep(1)
a = funa()
b = funb()
while True:
    print(next(a))
    print(next(b))


